<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
// require '../FileController.php';

class SeckimgController extends Controller
{
  //秒杀商品类别列表
  public function getIndex($id)
  {
    $wenzi = DB::table('seckill')->where('id',$id)->first()->name;
    $cate = DB::table('seckimg')->where('id',$id)->get();

    return view('admin.seckimg.index',['cate'=>$cate,'wenzi'=>$wenzi]);
  }

  //删除
  public function getDelete($id)
  {
    $res = DB::table('seckillglobal')->where('seckillid',$id)->count();
    if(!$res){
      return back();
    }
    $r = DB::table('seckimg')->where('sid',$id)->delete();
    if($r){
      return back()->with('success','删除成功');
    }else{
      return back()->with('error','删除失败');
    }
  }
  //修改
  public function getEdit($id)
  {
    $cates = DB::table('seckill')->where('id',$id)->first();
    return view('admin.seckill.edit',['cates'=>$cates]);
  }
  //执行修改
  public function postUpdate(Request $request)
  {
    $id = $request->input('id');
    $arr = $request->except(['pic','_token','id']);;
    if($arr['name'] == ''){
      return back()->withInput();
    }
    $res = DB::table('seckill')->where('id',$id)->update($arr);
    if(!$res){
      return back()->with('error','修改失败');
    }

    $obj = new FileUpload();
    $obj->upload('pic');
    $r = $obj->getFileName();
    $data['id'] = $id;
    DB::table('seckimg')->where('id',$id)->delete();
    foreach($r as $k => $v){
      $data['imginfo'] = $v;
      $srec = DB::table('seckimg')->insert($data);
    }
    if($srec){
      return back()->with('success','修改成功');
    }else{
      return back()->with('error','修改失败');
    }
  }
}